import requests
import json
import xlrd

def import_data():
    wb = xlrd.open_workbook('D:/PyProject/SinoMed/Test1.xls')
    ws = wb.sheets()[0]
    nrows = ws.nrows
    # print(nrows)
    company_names = ws.col_values(1, 1, nrows)
    company_ids = ws.col_values(2, 1, nrows)
    exchanges = ws.col_values(3, 1, nrows)
    return company_names, company_ids, exchanges

def statement_list(company_id):
    art_codes = []
    titles = []
    types = []
    type_codes = []

    url = "http://np-anotice-stock.eastmoney.com/api/security/ann?cb=jQuery112306462540294489227_1644457886069&sr=-1&page_size=200&page_index=1&ann_type=A&client_source=web&stock_list=" + str(company_id) + "&f_node=1&s_node=0"
    print(url)
    r = requests.get(url)
    content = r.text[42:-1]
    #print(content)
    data_list = json.loads(content).get('data').get('list')
    print(len(data_list))
    for data in data_list:
        #print(data)
        art_code = data.get('art_code')
        art_codes.append(art_code)
        title = data.get('title_ch')
        titles.append(title)
        type = data.get('columns')[0].get('column_name')
        types.append(type)
        type_code = data.get('columns')[0].get('column_code')
        type_codes.append(type_code)
    #print(art_codes)
    #print(titles)
    #print(types)
    #print(type_codes)
    #print(json.dumps(data, ensure_ascii=False, sort_keys=False, indent=4, separators=(', ', ': ')))
    return art_codes, titles, types, type_codes

def downloader(url, i):
    r = requests.get(url)
    print(titles[i].replace(":", ""))

    with open(titles[i].replace(":", "") + ".pdf", "wb") as code:
        code.write(r.content)
        print("done")

if __name__ == '__main__':

    company_names, company_ids, exchanges = import_data()
    #print(company_ids)

    company_id = "600519"
    art_codes, titles, types, type_codes = statement_list(company_id)

    for i, art_code in enumerate(art_codes):
        print(art_code)
        url = "https://pdf.dfcfw.com/pdf/H2_" + art_code + "_1.pdf"
        downloader(url, i)
        break
    #for art_code in art_codes:
